//
//  PageController.m
//  XCC
//
//  Created by 红沙尘 on 2019/6/10.
//  Copyright © 2019 红沙尘. All rights reserved.
//

#import "PageController.h"
#import "AController.h"
#import "BController.h"
#import "CController.h"
@interface PageController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource> {
    NSArray *viewControllers;
    __weak IBOutlet UIButton *bb;
    
    __weak IBOutlet UIView *v;
}

@end

@implementation PageController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    return [super initWithNibName:@"PageController" bundle:nibBundleOrNil];
}

- (instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(NSDictionary<UIPageViewControllerOptionsKey,id> *)options {
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
    
    return [super initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:dict];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    viewControllers = @[[[AController alloc] init],[[BController alloc] init],[[CController alloc] init]];
    
    [self setViewControllers:@[viewControllers[0]]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO
                  completion:nil];
    self.delegate   = self;
    self.dataSource = self;

    UIView *av = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    av.backgroundColor = UIColor.redColor;
    [self.view addSubview:av];
    [self.view bringSubviewToFront:v];

    [self.view bringSubviewToFront:bb];
}

#pragma mark
#pragma mark ----- UIPageViewControllerDataSource -----
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    
    NSUInteger index = [viewControllers indexOfObject:viewController];
    if (index == 0) {
        index = [viewControllers count] - 1;
    } else {
        index--;
    }
//    self.nowShowVCIndex = index;
    return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [self indexForViewController:viewController];
    index++;
    if (index == [viewControllers count]) {
        index = 0;
    }
//    self.nowShowVCIndex = index;
    return [self viewControllerAtIndex:index];
}
- (NSUInteger)indexForViewController:(UIViewController *)viewController {
    return [viewControllers indexOfObject:viewController];
}

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (index > [viewControllers count]) {
        return nil;
    }
    UIViewController *vc = [viewControllers objectAtIndex:index];
    return vc;
}

#pragma mark
#pragma mark ----- UIPageViewControllerDelegate -----

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (finished) {
        NSUInteger index = [self indexForViewController:[pageViewController.viewControllers firstObject]];
        [self configTopBarToIndex:index];
    }
}

- (void)configTopBarToIndex:(NSInteger)index {
    NSLog(@"index = %d",(int)index);
}


@end
